package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.scheduling.dao.SchedulingAttendanceDailyApplyDao;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceDailyApplyDto;
import com.quectel.core.module.scheduling.entity.SchedulingAttendanceDailyApplyEntity;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceDailyApplyService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/07/27 10:57
 */
@DubboService
public class SchedulingAttendanceDailyApplyServiceImpl extends ServiceImpl<SchedulingAttendanceDailyApplyDao, SchedulingAttendanceDailyApplyEntity> implements SchedulingAttendanceDailyApplyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAttendanceDailyApplyServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_ID:%d", id);

    /**
     * 入参解释:考勤记录 id
     */
    private static final Function<Long, String> SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_DAILY_ID_FUNC = dailyId -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_DAILY_ID:%d", dailyId);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingAttendanceDailyApplyDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingAttendanceDailyApplyDao schedulingAttendanceDailyApplyDao;


    @Override
    public void deleteByDailyId(Long dailyId) {
        schedulingAttendanceDailyApplyDao.delete(new LambdaQueryWrapper<SchedulingAttendanceDailyApplyEntity>()
                .eq(SchedulingAttendanceDailyApplyEntity::getDailyId, dailyId));
    }

    @Override
    public void saveBatch(List<SchedulingAttendanceDailyApplyDto> dtos) {
        List<SchedulingAttendanceDailyApplyEntity> entities = CopyUtils.copyList(dtos, SchedulingAttendanceDailyApplyEntity.class);
        for (SchedulingAttendanceDailyApplyEntity entity : entities) {
            entity.setId(Snowflake.nextId());
        }
        super.saveBatch(entities);
    }

    @Override
    public List<SchedulingAttendanceDailyApplyDto> selectCacheByDailyId(Long dailyId) {
        return CacheKit.cacheToRedis(
                () -> selectByDailyId(dailyId),
                SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_DAILY_ID_FUNC.apply(dailyId),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<SchedulingAttendanceDailyApplyDto> selectByDailyId(Long dailyId) {
        List<SchedulingAttendanceDailyApplyEntity> schedulingAttendanceDailyApplyEntities = schedulingAttendanceDailyApplyDao.selectList(new LambdaQueryWrapper<SchedulingAttendanceDailyApplyEntity>()
                .eq(SchedulingAttendanceDailyApplyEntity::getDailyId, dailyId));
        List<SchedulingAttendanceDailyApplyDto> applyDtos = CopyUtils.copyList(schedulingAttendanceDailyApplyEntities, SchedulingAttendanceDailyApplyDto.class);
        applyDtos.forEach(this::paddingField);
        return applyDtos;
    }

    @Override
    public SchedulingAttendanceDailyApplyDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_ATTENDANCE_DAILY_APPLY_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingAttendanceDailyApplyDto selectById(Long id) {
        SchedulingAttendanceDailyApplyEntity entity = schedulingAttendanceDailyApplyDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SchedulingAttendanceDailyApplyDto.class));
    }

    @Override
    public List<SchedulingAttendanceDailyApplyDto> queryList(Map<String, Object> params) {
        List<SchedulingAttendanceDailyApplyEntity> list = schedulingAttendanceDailyApplyDao.queryList(params);
        List<SchedulingAttendanceDailyApplyDto> result = CopyUtils.copyList(list, SchedulingAttendanceDailyApplyDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingAttendanceDailyApplyDto> queryListWithoutPadding(Map<String, Object> params) {
        List<SchedulingAttendanceDailyApplyEntity> list = schedulingAttendanceDailyApplyDao.queryList(params);
        return CopyUtils.copyList(list, SchedulingAttendanceDailyApplyDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingAttendanceDailyApplyDto paddingField(SchedulingAttendanceDailyApplyDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return schedulingAttendanceDailyApplyDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingAttendanceDailyApplyDto dto) {
        SchedulingAttendanceDailyApplyEntity entity = CopyUtils.copyObj(dto, SchedulingAttendanceDailyApplyEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingAttendanceDailyApplyDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingAttendanceDailyApplyDto dto) {
        SchedulingAttendanceDailyApplyEntity entity = CopyUtils.copyObj(dto, SchedulingAttendanceDailyApplyEntity.class);

        schedulingAttendanceDailyApplyDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingAttendanceDailyApplyDto dto) {
        SchedulingAttendanceDailyApplyEntity entity = CopyUtils.copyObj(dto, SchedulingAttendanceDailyApplyEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, schedulingAttendanceDailyApplyDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingAttendanceDailyApplyDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
